﻿using QQZiFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProject1
{
    public class PermissionSlutionData : IDBEntity
    {
        public virtual QueryBuilder Builder { get; set; }

        [DBField(Usage = DBFieldUsage.NoField)]
        public string ExtName { get { return Builder.ExtName; } }
        public string Permission { get; set; }
        public string QueryField { get; set; }

        [DBField(Usage = DBFieldUsage.NoField)]
        public string PrimaryKey { get { return Builder.PrimaryKey; } }

        //固定的条件，比如groupby
        public virtual System.Collections.Generic.Dictionary<string, string> WhereObject { get; set; }

        public string GroupByFields { get; set; }
        public string EntityName { get; set; }
        public bool IsTree { get; set; }

        public PermissionSlutionData() { }

        public QueryParam ToQueryParam(string clientQueryField, int pageIndex, int pagesize, DBContext context)
        {
            QueryParam param = context.CreateQueryBuilder().Permission(Permission);
            param.entityType = typeof(EmptyEntity);
            if (string.IsNullOrEmpty(clientQueryField)) clientQueryField = QueryField;
            else clientQueryField = clientQueryField.Replace("*", QueryField);
            param.queryField = clientQueryField;
            if (WhereObject != null)
            {
                foreach (var item in WhereObject) param.AddWhere(item.Key, item.Value);
            }
            if (pagesize > 0)
            {
                param.Page = new Pagger() { PageIndex = pageIndex, PageSize = pagesize };
                if (!string.IsNullOrEmpty(GroupByFields))
                {

                    param.Page.SetGroupByFields(GroupByFields);
                }
            }
            return param;
        }
    }
}
